```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>平衡二叉树算法解析 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.8;
            color: #333;
        }
        .hero {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .drop-cap::first-letter {
            font-size: 3.5rem;
            line-height: 0.8;
            float: left;
            margin-right: 0.5rem;
            font-weight: 700;
            color: #4f46e5;
        }
        .code-block {
            background-color: #f8f9fa;
            border-left: 4px solid #4f46e5;
        }
        .concept-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .footer-links a:hover {
            color: #a5b4fc;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-4xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-2/3">
                    <h1 class="text-4xl md:text-5xl font-bold mb-4">平衡二叉树算法解析</h1>
                    <p class="text-xl mb-8 opacity-90">深入理解AVL树的平衡原理与实现</p>
                    <div class="flex items-center">
                        <div class="bg-white text-purple-600 rounded-full p-2 mr-3">
                            <i class="fas fa-code text-xl"></i>
                        </div>
                        <p class="text-sm opacity-80">数据结构与算法 · 二叉树专题</p>
                    </div>
                </div>
                <div class="md:w-1/3 mt-10 md:mt-0">
                    <div class="bg-white bg-opacity-20 rounded-2xl p-6 backdrop-filter backdrop-blur-sm">
                        <h3 class="font-bold text-lg mb-3">关键概念</h3>
                        <ul class="space-y-2">
                            <li class="flex items-center">
                                <i class="fas fa-check-circle mr-2 text-green-300"></i>
                                <span>高度平衡条件</span>
                            </li>
                            <li class="flex items-center">
                                <i class="fas fa-check-circle mr-2 text-green-300"></i>
                                <span>递归遍历策略</span>
                            </li>
                            <li class="flex items-center">
                                <i class="fas fa-check-circle mr-2 text-green-300"></i>
                                <span>时间复杂度分析</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-4xl px-4 md:px-0 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <p class="drop-cap text-gray-700 mb-6">判断一个二叉树是否为平衡二叉树是一个常见的算法问题。平衡二叉树（AVL树）定义为每个节点的左右子树的高度差不超过1。要判断一棵二叉树是否平衡，可以使用递归方法，同时计算树的高度。</p>
            
            <div class="bg-indigo-50 border-l-4 border-indigo-500 p-4 my-6 rounded">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <i class="fas fa-info-circle text-indigo-500 text-xl"></i>
                    </div>
                    <div class="ml-3">
                        <p class="text-sm text-indigo-700">平衡二叉树在数据库索引、内存分配等场景中有广泛应用，理解其判定算法是掌握高级数据结构的基础。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 1 -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="bg-indigo-100 text-indigo-600 w-10 h-10 rounded-full flex items-center justify-center mr-4">1</span>
                平衡二叉树的定义
            </h2>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-8">
                <div class="p-6">
                    <h3 class="text-xl font-semibold text-gray-800 mb-4">平衡条件</h3>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <span class="flex-shrink-0 bg-indigo-100 text-indigo-600 rounded-full w-6 h-6 flex items-center justify-center mr-3 mt-1">
                                <i class="fas fa-check text-xs"></i>
                            </span>
                            <span>对于每个节点，左子树和右子树的高度差的绝对值不超过1</span>
                        </li>
                        <li class="flex items-start">
                            <span class="flex-shrink-0 bg-indigo-100 text-indigo-600 rounded-full w-6 h-6 flex items-center justify-center mr-3 mt-1">
                                <i class="fas fa-check text-xs"></i>
                            </span>
                            <span>每个子树也必须是平衡的</span>
                        </li>
                    </ul>
                </div>
            </div>

            <div class="my-8">
                <div class="mermaid">
                    graph TD
                        A[根节点] --> B[左子树]
                        A --> C[右子树]
                        B -->|高度差 ≤1| C
                        B --> D[...]
                        C --> E[...]
                </div>
            </div>
        </section>

        <!-- Section 2 -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="bg-indigo-100 text-indigo-600 w-10 h-10 rounded-full flex items-center justify-center mr-4">2</span>
                判断平衡二叉树的思路
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6 mb-8">
                <div class="concept-card bg-white rounded-xl shadow-md p-6 transition-all duration-300">
                    <div class="text-indigo-500 mb-4">
                        <i class="fas fa-sitemap text-3xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-gray-800 mb-3">递归方法</h3>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-arrow-right text-xs text-indigo-400 mt-1 mr-2"></i>
                            <span>采用深度优先搜索（DFS）遍历树</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-arrow-right text-xs text-indigo-400 mt-1 mr-2"></i>
                            <span>递归计算每个节点的左右子树高度</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-arrow-right text-xs text-indigo-400 mt-1 mr-2"></i>
                            <span>检查高度差是否符合平衡条件</span>
                        </li>
                    </ul>
                </div>
                
                <div class="concept-card bg-white rounded-xl shadow-md p-6 transition-all duration-300">
                    <div class="text-indigo-500 mb-4">
                        <i class="fas fa-tachometer-alt text-3xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-gray-800 mb-3">优化高度计算</h3>
                    <p class="text-gray-700">在递归函数中，如果发现不平衡，可以提前终止后续的高度计算，以提高算法效率。</p>
                </div>
            </div>
        </section>

        <!-- Section 3 -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="bg-indigo-100 text-indigo-600 w-10 h-10 rounded-full flex items-center justify-center mr-4">3</span>
                具体实现
            </h2>
            
            <div class="bg-gray-800 rounded-xl overflow-hidden mb-8">
                <div class="flex items-center px-4 py-2 bg-gray-700">
                    <div class="flex space-x-2 mr-4">
                        <span class="w-3 h-3 rounded-full bg-red-500"></span>
                        <span class="w-3 h-3 rounded-full bg-yellow-500"></span>
                        <span class="w-3 h-3 rounded-full bg-green-500"></span>
                    </div>
                    <span class="text-gray-300 text-sm">BalancedBinaryTree.java</span>
                </div>
                <pre class="p-4 text-gray-300 overflow-x-auto"><code>class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

public class BalancedBinaryTree {

    public boolean isBalanced(TreeNode root) {
        return checkBalance(root) != -1;
    }

    // 检查树是否平衡，并返回其高度
    private int checkBalance(TreeNode node) {
        // 基本情况：空树的高度为0
        if (node == null) {
            return 0;
        }

        // 递归检查左子树的高度
        int leftHeight = checkBalance(node.left);
        if (leftHeight == -1) { // 如果左子树不平衡，提前返回
            return -1;
        }

        // 递归检查右子树的高度
        int rightHeight = checkBalance(node.right);
        if (rightHeight == -1) { // 如果右子树不平衡，提前返回
            return -1;
        }

        // 检查当前节点的平衡性
        if (Math.abs(leftHeight - rightHeight) > 1) {
            return -1; // 不平衡，返回-1
        }

        // 返回当前节点的高度
        return Math.max(leftHeight, rightHeight) + 1;
    }

    public static void main(String[] args) {
        // 测试用例
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        root.left.left.left = new TreeNode(6); // 添加一个节点使其不平衡

        BalancedBinaryTree tree = new BalancedBinaryTree();
        System.out.println("Is the tree balanced? " + tree.isBalanced(root));
    }
}</code></pre>
            </div>
            
            <div class="bg-blue-50 border-l-4 border-blue-500 p-4 rounded">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <i class="fas fa-lightbulb text-blue-500 text-xl"></i>
                    </div>
                    <div class="ml-3">
                        <h4 class="text-sm font-bold text-blue-800">算法解析</h4>
                        <p class="text-sm text-blue-700">此实现通过递归方法巧妙地将高度计算与平衡检查合二为一，当检测到不平衡时立即返回-1，避免不必要的计算。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 4 -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="bg-indigo-100 text-indigo-600 w-10 h-10 rounded-full flex items-center justify-center mr-4">4</span>
                时间复杂度与空间复杂度分析
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white rounded-xl shadow-md p-6">
                    <h3 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
                        <i class="fas fa-stopwatch text-indigo-500 mr-3"></i>
                        时间复杂度
                    </h3>
                    <p class="text-gray-700 mb-2"><span class="font-bold">O(N)</span>，其中 N 是树中节点的总数。</p>
                    <p class="text-gray-600 text-sm">每个节点在递归中只访问一次，因此时间复杂度为线性。</p>
                    <div class="mt-4">
                        <div class="h-2 bg-gray-200 rounded-full overflow-hidden">
                            <div class="h-full bg-indigo-500 rounded-full" style="width: 100%"></div>
                        </div>
                        <div class="flex justify-between text-xs text-gray-500 mt-1">
                            <span>0</span>
                            <span>N (节点数量)</span>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6">
                    <h3 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
                        <i class="fas fa-memory text-indigo-500 mr-3"></i>
                        空间复杂度
                    </h3>
                    <p class="text-gray-700 mb-2"><span class="font-bold">O(H)</span>，其中 H 是树的高度。</p>
                    <p class="text-gray-600 text-sm">递归调用栈的空间取决于树的高度，最坏情况下(完全不平衡)H=N，最好情况(完全平衡)H=log(N)。</p>
                    <div class="mt-4 flex space-x-4">
                        <div>
                            <p class="text-xs text-gray-500 mb-1">最坏情况</p>
                            <div class="h-2 bg-gray-200 rounded-full overflow-hidden">
                                <div class="h-full bg-red-500 rounded-full" style="width: 100%"></div>
                            </div>
                        </div>
                        <div>
                            <p class="text-xs text-gray-500 mb-1">最好情况</p>
                            <div class="h-2 bg-gray-200 rounded-full overflow-hidden">
                                <div class="h-full bg-green-500 rounded-full" style="width: 30%"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Summary Section -->
        <section class="bg-indigo-50 rounded-2xl p-8 mb-16">
            <div class="flex flex-col md:flex-row">
                <div class="md:w-1/3 mb-6 md:mb-0 flex justify-center">
                    <div class="bg-white rounded-full w-32 h-32 flex items-center justify-center shadow-md">
                        <i class="fas fa-tree text-5xl text-indigo-500"></i>
                    </div>
                </div>
                <div class="md:w-2/3">
                    <h3 class="text-2xl font-bold text-gray-800 mb-4">关键要点总结</h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <div class="flex-shrink-0 mt-1">
                                <i class="fas fa-circle text-xs text-indigo-500"></i>
                            </div>
                            <p class="ml-2 text-gray-700">平衡二叉树的定义要求每个节点的左右子树高度差不超过1</p>
                        </li>
                        <li class="flex items-start">
                            <div class="flex-shrink-0 mt-1">
                                <i class="fas fa-circle text-xs text-indigo-500"></i>
                            </div>
                            <p class="ml-2 text-gray-700">采用后序遍历递归方法可以高效地同时检查平衡性和计算高度</p>
                        </li>
                        <li class="flex items-start">
                            <div class="flex-shrink-0 mt-1">
                                <i class="fas fa-circle text-xs text-indigo-500"></i>
                            </div>
                            <p class="ml-2 text-gray-700">算法的时间复杂度为O(N)，空间复杂度为O(H)</p>
                        </li>
                        <li class="flex items-start">
                            <div class="flex-shrink-0 mt-1">
                                <i class="fas fa-circle text-xs text-indigo-500"></i>
                            </div>
                            <p class="ml-2 text-gray-700">提前终止机制可以在发现不平衡时立即返回，提高效率</p>
                        </li>
                    </ul>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto max-w-4xl px-4 md:px-0">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-6 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                    <p class="text-sm">专注技术分享与算法解析</p>
                </div>
                <div class="footer-links">
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-indigo-400 transition duration-300">
                        <i class="fas fa-globe mr-2"></i>语雀主页
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-sm">
                <p>© 2023 技术小馆. 保留所有权利.</p>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'dark',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```